package org.aswing.util.map {
	import flash.utils.Dictionary;
	
	public class HaspMap implements IMap {
		private var keyArr:Array;
		private var dictionary:Dictionary;
		
		public function HaspMap() {
			this.clear();
		}
		
		public function clear():void {
			keyArr = [];
			dictionary = new Dictionary();
		}
		
		public function containsKey(key:Object):Boolean {
			return dictionary[key] != null;
		}
		
		public function containsValue(value:Object):Boolean {
			var result:Boolean = false;
			
			var len:uint = this.size();
			if(len > 0){
				for(var i:int=0; i<len; i++){
					if(dictionary[keyArr[i]]==value) return true;
				}
			}
			return result;
		}
		
		public function get(key:Object):Object {
			return dictionary[key];
		}
		
		public function put(key:Object, value:Object):Object {
			var result:Object = null;
			if(containsKey(key)){
				result = this.get(key);  //已含有该key则返回值 并重新赋值
				dictionary[key] = value;
			}else{
				dictionary[key] = value;
				keyArr.push(key);
			}
			return result;
		}
		
		public function putAll(map:IMap):void {
			this.clear();
			var len:uint = map.size();
			if(len>0){
				var arr:Array = map.keys();
				for(var i:int = 0; i<len; i++){
					this.put(arr[i],map.get(arr[i]));
				}
			}
		}
		
		public function remove(key:Object):void {
			if(containsKey(key)){
				delete dictionary[key];
				var index:int = keyArr.indexOf(key);
				if(index > -1){
					keyArr.slice(index,1);
				}
			}
		}
		
		public function size():int {
			return this.keyArr.length;
		}
		
		public function isEmpty():Boolean {
			
			return this.size() < 1;
		}
		
		public function values():Array {
			var result:Array = [];
			var len:uint = this.size();
			if(len > 0){
				for(var i:int; i<len; i++){
					result.push(dictionary[keyArr[i]]);
				}
			}
			return result;
		}
		
		public function keys():Array {
			return this.keyArr;
		}
	}
}